<?php

/**
  * Parse HTML Attributes
  *
  * Turns an array of attributes into a string
  *
  * @access		private
  * @param		array		attributes to be parsed
  * @return		string 		string of html attributes
  */
function _parse_asset_html($attributes = NULL)
{
	$attribute_str = '';

	if(is_string($attributes))
	{
		$attribute_str = $attributes;
	}
	
	else if(is_array($attributes) || is_object($attributes))
	{
		foreach($attributes as $key => $value)
		{
			$attribute_str .= ' '.$key.'="'.$value.'"';
		}
	}

	return $attribute_str;
}

function css($asset_name, $attributes = array())
{	
	global $CI;
	$attribute_str = _parse_asset_html($attributes);

	if(!preg_match('/rel="([^\"]+)"/', $attribute_str))
	{
		$attribute_str .= ' rel="stylesheet"';
	}
	
	return '<link href="'.STYLES_PATH.$CI->userTheme.$asset_name.'" type="text/css"'.$attribute_str.' />' . "\n";
}

function image($image_name, $attributes = array())
{
	global $CI;
	// No alternative text given? Use the filename, better than nothing!
	if(empty($attributes['alt'])) list($attributes['alt']) = explode('.', $image_name);
	
	$attribute_str = _parse_asset_html($attributes);

	return '<img src="'.THEME_PATH.$CI->userTheme.$image_name.'"'.$attribute_str.' />'."\n";
}

function img($image_name, $attributes = array()){
	return image($image_name, $attributes);
}

function js($js_name)
{
	return '<script type="text/javascript" src="'.SCRIPTS_PATH.$js_name.'"></script>'."\n";
}

function js_local($js_name){
	return '<script type="text/javascript" src="'.site_url("/common/loadjs/".endcodePath($js_name)).'"></script>'."\n";
}

function css_local($js_name){
	return '<script type="text/javascript" src="'.site_url("/common/loadcss/".endcodePath($js_name)).'"></script>'."\n";
}

function endcodePath($strJSPath){
	return str_replace('/','-',$strJSPath);	
}

function decodePath($strJSPath){
	return str_replace('-','/',$strJSPath);	
}


function js_content($js_name) {
	return file_get_contents(SCRIPTS_PATH . $js_name);
}

function css_content($css_name) {
	return file_get_contents(STYLES_PATH . $css_name);
}


function random_password($length = 8) {
	//settings
	$chars="ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz";
	
	//rest of script
	$escapecharplus=0;
	while ($escapecharplus < $length)
	{
		$randomword .= $chars[mt_rand(1, strlen($chars)-1)];
		$escapecharplus += 1;
	}
	
	return $randomword;
}

function send_text_mail($from, $to, $bcc, $subject, $text, $reply=false) {

	if (!$reply) $reply = $from;

	// headers
	$headers = '';
	$headers .= 'From: "'.$from.'" <'.$from.'>'."\n";
	$headers .= "Reply-To: ".$reply."\n";
	$headers .= "Return-Path: <".$reply.">\n";
	$headers .= "X-Sender: <".$from.">\n";
	if ( $bcc ) $headers .= 'BCC: "'.$bcc.'" <'.$bcc.'>'."\n";
	$headers .= "X-Mailer: PHP\n";
	$headers .= "X-Priority: 3\n";
	$headers .= "Content-Type: text/plain; charset=\"UTF-8\"\n";
	
	return mail_utf8($to, $subject, $text, $headers);
}

function mail_utf8($to, $subject = '(No subject)', $message = '', $header = '') {
	$header_ = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/plain; charset=UTF-8' . "\r\n";
	return mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=', $message, $header_ . $header);
}

?>
